#include<iostream>
using namespace std;
#include<vector>
#include<queue>

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {}
    
};
class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        vector<vector<int>> vv;
        queue<TreeNode*>q;
        if (root)
            q.push(root);
        while (!q.empty())
        {
            vector<int> v;
            int size = q.size();
            for (int i = 0; i < size; i++)
            {
                TreeNode* cur = q.front();
                q.pop();
                v.push_back(cur->val);
                if (root->left)
                    q.push(cur->left);
                if (root->right)
                    q.push(cur->right);
            }
            vv.push_back(v);
        }

        reverse(vv.begin(), vv.end());
        return vv;
    }
};
int main()
{
    TreeNode node7(7), node15(15), node20(20, &node15, &node7), node9(9), root(3, &node9, &node20);
    Solution s;
    s.levelOrderBottom(&root);
	return 0;
}